package org.panda.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServer {
    @Autowired
    UserRepository userRepository;

    public void addUser() {
        User user = new User();
        user.setName("John");
        user.setEmail("john@example.com");
        userRepository.save(user);
    }

    public void findAllUsers() {
        List<User> users = userRepository.findAll();
        users.forEach(user -> System.out.println(user.getName() + " - " + user.getEmail()));
    }
    public void findUserByName() {
        List<User> users = userRepository.findByName("John");
        users.forEach(user -> System.out.println(user.getName() + " - " + user.getEmail()));
    }

    public void updateUser() {
        User user = userRepository.findByName("John").get(0);
        user.setEmail("john.updated@example.com");
        userRepository.save(user);
    }

    public void deleteUser() {
        User user = userRepository.findByName("John").get(0);
        userRepository.delete(user);
    }

}
